<?php
class Page {
	
	private $currentPage;	 // 当前页数
	
	private $pageSize;		 // 每页显示数
		
	private $pageAmount;	 // 总页数
	
	private $dataAmount;	 // 数据总数
	
	private $maxShowPageNum; // 最大显示分页页数个

	private $pagingStyleClassName;	// 分页条样式类名
	
	public function __construct($current_page, $data_amount, $page_size=10, $max_show_pagenum=10) {
		$this->pageSize = $page_size;
		$this->setDataAmount($data_amount);
		$this->setCurrentPage($current_page);
		$this->maxShowPageNum = $max_show_pagenum;
	}
	
	// 设定当前页数
	public function setCurrentPage($current_page) {
		if ($current_page < 1 || !is_numeric($current_page) || strlen($current_page) != strlen((int)$current_page))
			$current_page = 1;
		if ($current_page > $this->pageAmount)
			$current_page = $this->pageAmount;
		
		$this->currentPage = $current_page;
	}
	
	// 得到当前页数
	public function getCurrentPage() {
		return $this->currentPage;
	}
	
	// 设定总数据数
	public function setDataAmount($data_amount) {
		$this->dataAmount =  $data_amount;
		$this->setPageAmount();				// 设定总数据数后，设定总页数
	}
	
	// 设定总页数
	private function setPageAmount() {
		$this->pageAmount = ceil($this->dataAmount / $this->pageSize);
	}
	
	// 得到总页数
	public function getPageAmount() {
		return $this->pageAmount;
	}
	
	// 设定分页条样式类名 <div id="paging" class="xxx">
	public function setPagingStyleClassName($class_name) {
		$this->pagingStyleClassName = $class_name;
	}
	
	/**
	 * 返回分页字符串
	 *
	 * @param string $page_pname		分页参数名,用于传递当前页数
	 * @param string $other_params		其它参数
	 * @return string
	 */
	public function printPage($page_pname='page', $other_params='') {
		// 分页参数集合
		$full_url = $_SERVER['PHP_SELF'].'?';
		$page_params = !empty($other_params) ? $other_params .'&'.$page_pname : $page_pname;
		$full_url .= $page_params.'=';
		
		
		$page_html = '<div id="paging" class="'.$this->pagingStyleClassName.'"><div class="page-bottom">'; // 分页html
		$page_html .= $this->currentPage < 2  ? '<span class="disabled page_prev"><img alt="上一页" src="images/left_1.png"></span>' : '<a class="page_prev" href="'.$full_url.''.($this->currentPage-1).'#list"><img alt="下一页" src="images/left_1.png"></a> ';
		
		// 左边链接数
		// 让当前页数居于最中间 1 2 3 4 5 [6] 7 8 9 10 11 
		$left_link_num = ceil($this->maxShowPageNum / 2) - 1;
		if ($this->pageAmount > $this->maxShowPageNum && $this->currentPage > $this->maxShowPageNum - $left_link_num + 1) {
			$page_html .= '<a href="'.$full_url.'1#list">1</a>...';
		}
		
		if ($this->currentPage - $left_link_num < $left_link_num) 			// 当前页数还没有超过左边链接数
			$start_num = 1;													// 开始值设定为1
		else if ($this->pageAmount <= $this->maxShowPageNum) 
			$start_num = 1;
		else if ($this->pageAmount - $this->currentPage < $left_link_num) 	
			$start_num = $this->pageAmount - $this->maxShowPageNum + 1;			
		else
			$start_num = $this->currentPage - $left_link_num;				// 让其选中页值居中

		for ($i=$start_num; $i<$this->maxShowPageNum+$start_num; $i++) {
			if ($i > $this->pageAmount) break;
			if ($i == $this->currentPage) {
				$page_html .= '<span class="current">'.$i.'</span>';
			}else {
				$page_html .= '<a href="'.$full_url.''.$i.'#list">'.$i.'</a>';
			}
		}

		if ($this->pageAmount >$this->maxShowPageNum && $this->currentPage < $this->pageAmount - $left_link_num - 1) {
			$page_html .= '...<a href="'.$full_url.($this->pageAmount).'#list">'.($this->pageAmount).'</a>';
		}
		
		$page_html .= $this->currentPage == $this->pageAmount ? '<span class="disabled page_next"><img alt="下一页" src="images/right_1.png"></span>' : '<a href="'.$full_url.''.($this->currentPage+1).'#list" class="page_next"><img alt="下一页" src="images/right_1.png"></a>';
		
		$page_html .= "</div></div>";
		return $page_html;
		
		
	}
}
?>
